package com.org.android.diary.activity;

import com.org.android.diary.R;
import com.org.android.diary.adapter.ImageAdapter;
import com.org.android.diary.utils.LogUtil;
import com.org.android.diary.utils.TaoLinear;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 详细日记
 */
public class DetailDiaryInfoActivity extends Activity {
    private TextView title = null;
    private TextView date = null;
    private TextView info = null;
    private ImageView back = null;
    private SharedPreferences preferences = null;
    private RecyclerView recycler;
    private ImageAdapter imageAdapter;
    private List<String> listImgs = new ArrayList<>();
    private List<String> listImgsResult = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.detail_diary_info);

        recycler = findViewById(R.id.recycler);

        imageAdapter = new ImageAdapter(this, listImgs);
        TaoLinear taoLinear = new TaoLinear(this, 3);
        taoLinear.setScrollEnabled(false);
        recycler.setLayoutManager(taoLinear);
        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setAdapter(imageAdapter);

        preferences = getSharedPreferences("image", MODE_PRIVATE);
        title = (TextView) this.findViewById(R.id.detail_view_title);
        date = (TextView) this.findViewById(R.id.detail_view_date);
        info = (TextView) this.findViewById(R.id.detail_view_info);
        back = (ImageView) this.findViewById(R.id.back_detail_diary);
        Bundle bundle = this.getIntent().getExtras();
        title.setText(bundle.getString("title"));
        date.setText(bundle.getString("date") + "  " + bundle.getString("week") + "  " + bundle.getString("weather"));
        info.setText(bundle.getString("info"));
        String images = bundle.getString("imgs");
        if (images.contains(",")) {
            listImgsResult = Arrays.asList(images.split(","));
        }else{
            listImgsResult.clear();
            listImgsResult.add(images);
        }

        String music = bundle.getString("music");
        LogUtil.e("图片-->"+ bundle.getString("imgs"));
        LogUtil.e("语音-->"+ bundle.getString("music"));

        if (listImgsResult.size() != 0) {
            listImgs.clear();
            listImgs.addAll(listImgsResult);
            imageAdapter.notifyDataSetChanged();
        }

        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
        setBackground();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setBackground();
    }

    private void setBackground() {
        LinearLayout layout = (LinearLayout) this.findViewById(R.id.detail_diary_info_layout);
        int id = preferences.getInt("id", 0);
        if (id == 0) {
            layout.setBackgroundResource(R.drawable.diary_view_bg);
        } else if (id == 1) {
            layout.setBackgroundResource(R.drawable.diary_view_bg);
        } else if (id == 2) {
            layout.setBackgroundResource(R.drawable.spring);
        } else if (id == 3) {
            layout.setBackgroundResource(R.drawable.summer);
        } else if (id == 4) {
            layout.setBackgroundResource(R.drawable.autumn);
        } else if (id == 5) {
            layout.setBackgroundResource(R.drawable.winter);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
